package bb.lanxing.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import bb.lanxing.App;
import bb.lanxing.calc.data.HeartratePoint;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.antplus.AntPlusDataDispatcher;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.BleDataDispatcher;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.base.converter.HeartrateBeltConverter;
import bb.lanxing.lib.devices.base.converter.RadarConverter;
import bb.lanxing.lib.devices.bici.BiciController;
import bb.lanxing.lib.devices.remote.IRemoteDeviceServiceImpl;
import bb.lanxing.lib.devices.utils.AntPlusUtils;
import bb.lanxing.lib.devices.utils.DeviceManagerHelper;
import bb.lanxing.manager.SourcePointManager;
import bb.lanxing.util.Log;
import bb.lanxing.util.text.TextUtils;

public class DeviceService extends Service {
    private static final String TAG = "DeviceService";

    public static final String ACTION_CLOSE_BLE = "bb.lanxing.action_close_ble";
    public static final String ACTION_OPEN_BLE = "bb.lanxing.action_open_ble";

    private static boolean isBleOpened = false;
    private final HeartrateBeltConverter heartrateBeltConverter = new HeartrateBeltDataListener();
    private SourcePointManager sourcePointManager = SourcePointManager.getInstance();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("DeviceService: onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        String action = TextUtils.isEmptyOrNull(intent.getAction()) ? "" : intent.getAction();
        Log.d("DeviceService: " + action);
        if (action.equals(ACTION_CLOSE_BLE)) {
            closeBLE();
        } else if (action.equals(ACTION_OPEN_BLE)) {
            openBLE();
        }
        stopSelfResult(i2);
        return super.onStartCommand(intent, i, i2);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("DeviceService: onBind");
        openBLE();
        return new IRemoteDeviceServiceImpl();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("DeviceService: onDestroy");
    }

    private void openBLE() {
        if (!isBleOpened || DeviceContext.isReleased()) {
            Log.d("DeviceService: openBLE");
            isBleOpened = true;
            XZDeviceHelper.init(getApplicationContext());
            BiciController biciController = DeviceManagerHelper.getBiciController();

            if (biciController != null) {
                biciController.registerBiciListener(sourcePointManager.biciListener);
            }
            DeviceManager deviceManager = XZDeviceHelper.getDeviceManager();
            if (deviceManager != null) {
                deviceManager.registerConnectionStateListener(sourcePointManager.deviceListener);
            }
            BleDataDispatcher bleDataDispatcher = BleDataDispatcher.getInstance();
            bleDataDispatcher.addConverter(SmartDevice.TYPE_CADENCE, sourcePointManager.cadenceConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_HEARTRATE, sourcePointManager.heartrateConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_POWERRATE, sourcePointManager.powerrateConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_BIKE_TRAINER, sourcePointManager.biketrainerConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_INDOOR_BIKE, sourcePointManager.indoorbikeConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_CROSS_TRAINER, sourcePointManager.crosstrainerConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_ROWER, sourcePointManager.rowerConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_HEARTRATE_BELT, heartrateBeltConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_RADAR_SR, new RadarConverter());
            bleDataDispatcher.addConverter(SmartDevice.TYPE_HR_X2_PRO, heartrateBeltConverter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_CADENCE01, sourcePointManager.cadence01Converter);
            bleDataDispatcher.addConverter(SmartDevice.TYPE_HEART_RATE01, sourcePointManager.heartRate01Converter);
            if (AntPlusUtils.isAntPlusAvailable(App.getContext())) {
                AntPlusDataDispatcher.getInstance().registerAntPlusDataListener(sourcePointManager.cadenceConverter);
                AntPlusDataDispatcher.getInstance().registerAntPlusDataListener(sourcePointManager.heartrateConverter);
            }
        }
    }

    private void closeBLE() {
        if (!isBleOpened) {
            return;
        }
        Log.d("DeviceService: closeBLE");
        DeviceManager deviceManager = XZDeviceHelper.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.unregisterConnectionStateListener(sourcePointManager.deviceListener);
        }
        BleDataDispatcher.terminate();
        AntPlusDataDispatcher.terminate();
        XZDeviceHelper.release();
        isBleOpened = false;
    }

    private class HeartrateBeltDataListener extends HeartrateBeltConverter {
        private HeartrateBeltDataListener() {
        }

        @Override
        public void onHeartrate(int deviceType, int heartrate) {
            Log.v(TAG, "onHeartrate, heartrate = " + heartrate);
            HeartratePoint heartratePoint = new HeartratePoint();
            heartratePoint.setHeartrate(heartrate);
            heartratePoint.setSource(2);
            if (sourcePointManager.heartrateCalc != null) {
                sourcePointManager.heartrateCalc.calc(heartratePoint);
            }
            sourcePointManager.heartratePoint = heartratePoint;
        }
    }
}